import Vue from 'vue'
import VueRouter from 'vue-router'
//引入Login路由
import Login from '../components/Login.vue'
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import User from "../components/admin/User";
import PowerManage from "../components/admin/PowerManage";
import Sport from "../components/admin/Sport";

Vue.use(VueRouter)

const routes = [
    {
        path: "/",
        redirect: "/login"
    },
    {
        path: "/login",
        component: Login
    },
    {
        path: "/home",
        component: Home,
        redirect: "/welcome",
        children: [
            {path: "/welcome", component: Welcome,},
            {path: "/user", component: User,},
            {path: "/powerMange", component: PowerManage,},
            {path: "/sport", component: Sport}
        ]
    }
]

//创建了一个router对象
const router = new VueRouter({
    routes
})

/**
 * 挂载路由导航卫士   相当于过滤器判断
 to 将要访问
 from 从哪里访问
 next 跳转地址，接下来干嘛 next（url）如果有参就跳转到该url上，若是无参访问to路径
 */
router.beforeEach((to, from, next) => {
    //如果是登录地址，直接放行
    if (to.path == '/login') return next();
    //获取user
    const userFlag = window.sessionStorage.getItem("user");
    console.log("userFlag:" + userFlag);
    if (!userFlag) return next("/login"); //没有值返回登录页 即userFlag为null
    next();
})

export default router //暴露出去
